package com.haha.gulimall.product.service.impl;

import com.haha.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.haha.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.haha.gulimall.product.entity.AttrEntity;
import com.haha.gulimall.product.vo.AttrGroupRelationVo;
import com.haha.gulimall.product.vo.AttrGroupWhithAttrsVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.haha.common.utils.PageUtils;
import com.haha.common.utils.Query;

import com.haha.gulimall.product.dao.AttrGroupDao;
import com.haha.gulimall.product.entity.AttrGroupEntity;
import com.haha.gulimall.product.service.AttrGroupService;

import javax.annotation.Resource;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    private PageUtils pageUtils;
    @Resource
    AttrAttrgroupRelationDao attrAttrgroupRelationDao;
    @Resource
    AttrServiceImpl attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage1(Map<String, Object> params, Long catelogId) {
        String key = (String) params.get("key");
        QueryWrapper<AttrGroupEntity> qw = new QueryWrapper<AttrGroupEntity>();
        if(catelogId!=0){
          qw.eq("catelog_id", catelogId);
        }
        if (StringUtils.isNotBlank(key)) {
            qw.and(obj -> {
                obj.eq("attr_group_id", key).or().like("attr_group_name", key);
            });
        }
        IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), qw);
        return new PageUtils(page);


    }

    @Override
    public void deleteGroupRelation(AttrGroupRelationVo[] attrGroupRelationVo) {
        List<AttrAttrgroupRelationEntity> collect = Arrays.asList(attrGroupRelationVo).stream().map(obj -> {
            AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(obj, attrAttrgroupRelationEntity);
            return attrAttrgroupRelationEntity;
        }).collect(Collectors.toList());
        attrAttrgroupRelationDao.deleteBatch(collect);
    }

    /**
     * 得到三级分类的所有属性分组，并把属性详情也带出来
     */
    @Override
    public List<AttrGroupWhithAttrsVo> getAttrGroupWithAttrsByCatId(Long catelogId) {
        List<AttrGroupEntity> attrGroupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        List<AttrGroupWhithAttrsVo> collect = attrGroupEntities.stream().map(obj -> {
            AttrGroupWhithAttrsVo attrsVo = new AttrGroupWhithAttrsVo();
            BeanUtils.copyProperties(obj, attrsVo);
            List<AttrEntity> relationAttr = attrService.getRelationAttr(obj.getAttrGroupId());
            attrsVo.setAttrs(relationAttr);
            return attrsVo;
        }).collect(Collectors.toList());
        return collect;
    }
}